<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <good-img :mainImg="goodDetail.mainPictures" />
          <GoodSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodNames />
          <!-- Sku信息 -->
          <GoodsSku :goods="goodDetail" @change="getSku" />
          <!-- 数量加减 -->
          <!-- vue2.x 组件身上写了 v-model = "name"（语法糖） => :value + @input
          vue3.0 v-model = "name"（语法糖） => :modelValue + @update:modelValue -->
          <XtxNumbox v-model="buyNum" :max="goodDetail.inventory" />
          <!-- 按钮 -->
          <XtxButton
            style="margin-top: 20px"
            size="large"
            type="primary"
            @click="addCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img
              v-for="(img, i) in goodDetail.details.pictures"
              :key="i"
              v-imgLazy="img"
            />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoodDetail } from '@/api/good'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodImg from './components/goods-image.vue'
import GoodSales from './components/goods-sales.vue'
import GoodNames from './components/goods-name.vue'
import msg from '@/components/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodImg,
    GoodSales,
    GoodNames
  },
  setup () {
    // 双向绑定获取购买数量
    const buyNum = ref(1)
    const route = useRoute()
    const goodDetail = ref({})
    provide('goodDetail', goodDetail)
    async function getGoodDetail () {
      const res = await findGoodDetail(route.params.id)
      console.log('商品详情', res)
      goodDetail.value = res
      // provide('goodDetail', goodDetail) // 错误不支持嵌套 只能放在setup
    }
    onMounted(() => {
      getGoodDetail()
    })
    // 当前选中有效商品sku信息
    const currSel = ref(null)
    // 获取sku数据
    const getSku = sku => {
      console.log('sku:', sku)
      // 修改商品的现价原价库存信息
      if (sku.skuId) {
        goodDetail.value.price = sku.price
        goodDetail.value.oldPrice = sku.oldPrice
        goodDetail.value.inventory = sku.inventory
        // 存储有效信息
        currSel.value = sku
      } else {
        currSel.value = null
      }
    }
    // 加入购物车
    const store = useStore()
    const addCart = async () => {
      if (!currSel.value) {
        return msg({ text: '请选择商品规格！' })
      }
      if (currSel.value.inventory === 0) {
        return msg({ text: '库存不足' })
      }
      const addGood = {
        id: goodDetail.value.id,
        name: goodDetail.value.name,
        picture: goodDetail.value.mainPictures[0],
        skuId: currSel.value.skuId, // 唯一
        price: currSel.value.oldPrice,
        nowPrice: currSel.value.price,
        attrsText: currSel.value.specsText,
        stock: currSel.value.inventory,
        selected: true,
        isEffective: true,
        count: buyNum.value
      }
      console.log('购物信息', addGood)
      try {
        // await接受promise的返回值 cart.js addCartAction
        const rmsg = await store.dispatch('cart/addCartAction', addGood)
        msg({ text: rmsg, type: 'success' })
      } catch (error) {
        console.log(error)
      }
    }
    return { goodDetail, getSku, buyNum, addCart }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
